/*
 * Copyright (c) 2008 AnyWi Technologies
 * Author: Andrea Guzzo <aguzzo@anywi.com>
 * * based on uark.c 1.1 2006/08/14 08:30:22 jsg *
 * * parts from ubsa.c 183348 2008-09-25 12:00:56Z phk *
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * $FreeBSD: releng/11.2/sys/dev/usb/serial/u3g.c 331176 2018-03-19 04:03:55Z eadler $
 */

/*
 * NOTE:
 *
 * - The detour through the tty layer is ridiculously expensive wrt
 *   buffering due to the high speeds.
 *
 *   We should consider adding a simple r/w device which allows
 *   attaching of PPP in a more efficient way.
 *
 */

#ifdef USB_GLOBAL_INCLUDE_FILE
#include USB_GLOBAL_INCLUDE_FILE
#include "usb_serial.h"
#include "los_queue.h"
#endif

#undef USB_DEBUG_VAR
#define USB_DEBUG_VAR   u3g_debug
#ifdef LOSCFG_USB_DEBUG
static int u3g_debug = 0;
void u3g_debug_func(int level)
{
    u3g_debug = level;
    PRINTK("The level of u3g debug is %d\n", level);
}
DEBUG_MODULE(u3g, u3g_debug_func);
#endif

static int    bootverbose = 0;

#define    U3G_MAXPORTS        12
#define    U3G_CONFIG_INDEX    1
#define    U3G_BSIZE        2048
#define    U3G_TXSIZE        (U3G_BSIZE / U3G_TXFRAMES)
#define    U3G_TXFRAMES        4

#define    U3GSP_GPRS        0
#define    U3GSP_EDGE        1
#define    U3GSP_CDMA        2
#define    U3GSP_UMTS        3
#define    U3GSP_HSDPA        4
#define    U3GSP_HSUPA        5
#define    U3GSP_HSPA        6
#define    U3GSP_MAX        7

/* Eject methods; See also usb_quirks.h:UQ_MSC_EJECT_* */
#define    U3GINIT_HUAWEI        1    /* Requires Huawei init command */
#define    U3GINIT_SIERRA        2    /* Requires Sierra init command */
#define    U3GINIT_SCSIEJECT    3    /* Requires SCSI eject command */
#define    U3GINIT_REZERO        4    /* Requires SCSI rezero command */
#define    U3GINIT_ZTESTOR        5    /* Requires ZTE SCSI command */
#define    U3GINIT_CMOTECH        6    /* Requires CMOTECH SCSI command */
#define    U3GINIT_WAIT        7    /* Device reappears after a delay */
#define    U3GINIT_SAEL_M460    8    /* Requires vendor init */
#define    U3GINIT_HUAWEISCSI    9    /* Requires Huawei SCSI init command */
#define     U3GINIT_HUAWEISCSI2 10
#define     U3GINIT_HUAWEISCSI3 11
#define    U3GINIT_TCT        12    /* Requires TCT Mobile init command */

enum {
    U3G_BULK_WR,
    U3G_BULK_RD,
    U3G_INTR,
    U3G_N_TRANSFER,
};

struct u3g_softc {
    struct ucom_super_softc sc_super_ucom;
    struct ucom_softc sc_ucom[U3G_MAXPORTS];

    struct usb_xfer *sc_xfer[U3G_MAXPORTS][U3G_N_TRANSFER];
    uint8_t sc_iface[U3G_MAXPORTS];            /* local status register */
    uint8_t sc_lsr[U3G_MAXPORTS];            /* local status register */
    uint8_t sc_msr[U3G_MAXPORTS];            /* u3g status register */
    uint16_t sc_line[U3G_MAXPORTS];            /* line status */

    struct usb_device *sc_udev;
    struct mtx sc_mtx;

    uint8_t sc_numports;
};

static device_probe_t u3g_probe;
static device_attach_t u3g_attach;
static device_detach_t u3g_detach;
static void u3g_free_softc(struct u3g_softc *);

static usb_callback_t u3g_write_callback;
static usb_callback_t u3g_read_callback;
static usb_callback_t u3g_intr_callback;

static void u3g_cfg_get_status(struct ucom_softc *, uint8_t *, uint8_t *);
static void u3g_cfg_set_dtr(struct ucom_softc *, uint8_t);
static void u3g_cfg_set_rts(struct ucom_softc *, uint8_t);
static void u3g_start_read(struct ucom_softc *ucom);
static void u3g_stop_read(struct ucom_softc *ucom);
static void u3g_start_write(struct ucom_softc *ucom);
static void u3g_stop_write(struct ucom_softc *ucom);
static void u3g_poll(struct ucom_softc *ucom);
static void u3g_free(struct ucom_softc *ucom);

static const struct usb_config u3g_config[U3G_N_TRANSFER] = { /*lint -e26 -e546*/

    [U3G_BULK_WR] = {
        .type = UE_BULK,
        .endpoint = UE_ADDR_ANY,
        .direction = UE_DIR_OUT,
        .bufsize = U3G_BSIZE,/* bytes */
        .frames = U3G_TXFRAMES,
        .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
        .callback = &u3g_write_callback,
    },

    [U3G_BULK_RD] = {
        .type = UE_BULK,
        .endpoint = UE_ADDR_ANY,
        .direction = UE_DIR_IN,
        .bufsize = U3G_BSIZE,/* bytes */
        .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
        .callback = &u3g_read_callback,
    },

    [U3G_INTR] = {
        .type = UE_INTERRUPT,
        .endpoint = UE_ADDR_ANY,
        .direction = UE_DIR_IN,
        .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
        .bufsize = 0,    /* use wMaxPacketSize */
        .callback = &u3g_intr_callback,
    },
};

static const struct ucom_callback u3g_callback = {
    .ucom_cfg_get_status = &u3g_cfg_get_status,
    .ucom_cfg_set_dtr = &u3g_cfg_set_dtr,
    .ucom_cfg_set_rts = &u3g_cfg_set_rts,
    .ucom_start_read = &u3g_start_read,
    .ucom_stop_read = &u3g_stop_read,
    .ucom_start_write = &u3g_start_write,
    .ucom_stop_write = &u3g_stop_write,
    .ucom_poll = &u3g_poll,
    .ucom_free = &u3g_free,
};

static device_method_t u3g_methods[] = { /*lint -e611*/
    DEVMETHOD(device_probe, u3g_probe),
    DEVMETHOD(device_attach, u3g_attach),
    DEVMETHOD(device_detach, u3g_detach),
    DEVMETHOD_END
}; /*lint +e546*/

static devclass_t u3g_devclass;

static driver_t u3g_driver = {
    .name = "u3g",
    .methods = u3g_methods,
    .size = sizeof(struct u3g_softc),
};

DRIVER_MODULE(u3g, uhub, u3g_driver, u3g_devclass, 0, 0); /*lint !e19*/

static const STRUCT_USB_HOST_ID u3g_devs[] = {
    {USB_VPI(4817, 5569, 0)},
        {USB_VPI(4817, 5374, UQ_MSC_EJECT_HUAWEISCSI3)},
        {USB_VPI(4817, 5470, 0)},

};

static void
u3g_sael_m460_init(struct usb_device *udev)
{
    static const uint8_t setup[][24] = {
         { 0x41, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
         { 0x41, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 },
         { 0x41, 0x13, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
           0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
         { 0xc1, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x40, 0x02 },
         { 0xc1, 0x08, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00 },
         { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 },
         { 0xc1, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 },
         { 0x41, 0x01, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00 },
         { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 },
         { 0x41, 0x03, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00 },
         { 0x41, 0x19, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00,
           0x00, 0x00, 0x00, 0x00, 0x11, 0x13 },
         { 0x41, 0x13, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
           0x09, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
           0x0a, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00 },
         { 0x41, 0x12, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00 },
         { 0x41, 0x01, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00 },
         { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 },
         { 0x41, 0x03, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00 },
         { 0x41, 0x19, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00,
           0x00, 0x00, 0x00, 0x00, 0x11, 0x13 },
         { 0x41, 0x13, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
           0x09, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
           0x0a, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00 },
         { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 },
    };

    struct usb_device_request req;
    usb_error_t err;
    uint16_t len;
    uint8_t buf[0x300];
    uint8_t n;

    DPRINTFN(1, "\n");

    if (usbd_req_set_alt_interface_no(udev, (struct mtx *)NULL, 0, 0)) {
        DPRINTFN(0, "Alt setting 0 failed\n");
        return;
    }

    for (n = 0; n != (sizeof(setup)/sizeof(setup[0])); n++) {

        (void)memcpy_s(&req, sizeof(req), setup[n], sizeof(req));

        len = UGETW(req.wLength);
        if (req.bmRequestType & UE_DIR_IN) {
            if (len > sizeof(buf)) {
                DPRINTFN(0, "too small buffer\n");
                continue;
            }
            err = usbd_do_request(udev, (struct mtx *)NULL, &req, buf);
        } else {
            if (len > (sizeof(setup[0]) - 8)) {
                DPRINTFN(0, "too small buffer\n");
                continue;
            }
            err = usbd_do_request(udev, (struct mtx *)NULL, &req,
                __DECONST(uint8_t *, &setup[n][8]));
        }
        if (err) {
            DPRINTFN(1, "request %u failed\n",
                (unsigned int)n);
            /*
             * Some of the requests will fail. Stop doing
             * requests when we are getting timeouts so
             * that we don't block the explore/attach
             * thread forever.
             */
            if (err == USB_ERR_TIMEOUT)
                break;
        }
    }
}

static int
u3g_probe(device_t self)
{
    struct usb_attach_arg *uaa = (struct usb_attach_arg *)device_get_ivars(self);

    if (uaa->usb_mode != USB_MODE_HOST) {
        return (ENXIO);
    }

    if (uaa->info.bInterfaceClass != UICLASS_VENDOR) {
        return (ENXIO);
    }
    return (usbd_lookup_id_by_uaa(u3g_devs, sizeof(u3g_devs), uaa));
}

static int
u3g_attach(device_t dev)
{
    struct usb_config u3g_config_tmp[U3G_N_TRANSFER];
    struct usb_attach_arg *uaa = (struct usb_attach_arg *)device_get_ivars(dev);
    struct u3g_softc *sc = (struct u3g_softc *)device_get_softc(dev);
    struct usb_interface *iface;
    struct usb_interface_descriptor *id;
    uint32_t iface_valid;
    int error, type, nports;
    int ep, n;
    uint8_t i;

    DPRINTF("sc=%p\n", sc);

    type = USB_GET_DRIVER_INFO(uaa);
    if (type == U3GINIT_SAEL_M460
        || usb_test_quirk(uaa, UQ_MSC_EJECT_SAEL_M460)) {
        u3g_sael_m460_init(uaa->device);
    }

    /* copy in USB config */
    for (n = 0; n != U3G_N_TRANSFER; n++)
        u3g_config_tmp[n] = u3g_config[n];

    device_set_usb_desc(dev);
    mtx_init(&sc->sc_mtx, "u3g", NULL, MTX_DEF);
    ucom_ref(&sc->sc_super_ucom);

    sc->sc_udev = uaa->device;

    /* Claim all interfaces on the device */
    iface_valid = 0;
    for (i = uaa->info.bIfaceIndex; i < USB_IFACE_MAX; i++) {
        iface = usbd_get_iface(uaa->device, i);
        if (iface == NULL)
            break;
        id = usbd_get_interface_descriptor(iface);
        if (id == NULL || id->bInterfaceClass != UICLASS_VENDOR)
            continue;
        usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
        iface_valid |= (1<<i);
    }

    i = 0;        /* interface index */
    ep = 0;        /* endpoint index */
    nports = 0;    /* number of ports */
    while (i < USB_IFACE_MAX) {
        if ((iface_valid & (1<<i)) == 0) {
            i++;
            continue;
        }

        /* update BULK endpoint index */
        for (n = 0; n < U3G_N_TRANSFER; n++)
            u3g_config_tmp[n].ep_index = ep;

        /* try to allocate a set of BULK endpoints */
        error = usbd_transfer_setup(uaa->device, &i,
            sc->sc_xfer[nports], u3g_config_tmp, U3G_N_TRANSFER,
            &sc->sc_ucom[nports], &sc->sc_mtx);
        if (error) {
            /* next interface */
            i++;
            ep = 0;
            continue;
        }

        iface = usbd_get_iface(uaa->device, i);
        id = usbd_get_interface_descriptor(iface);
        sc->sc_iface[nports] = id->bInterfaceNumber;

        if (bootverbose && sc->sc_xfer[nports][U3G_INTR]) {
            device_printf(dev, "port %d supports modem control",
                      nports);
        }

        nports++;    /* found one port */
        ep++;
        if (nports == U3G_MAXPORTS)
            break;
    }
    if (nports == 0) {
        device_printf(dev, "no ports found\n");
        goto detach;
    }
    sc->sc_numports = nports;

    error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom,
        sc->sc_numports, sc, &u3g_callback, &sc->sc_mtx);
    if (error) {
        DPRINTF("ucom_attach failed\n");
        goto detach;
    }

    ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
    device_printf(dev, "Found %u port%s.\n", sc->sc_numports,
        sc->sc_numports > 1 ? "s":"");

    return (0);

detach:
    (void)u3g_detach(dev);
    return (ENXIO);
}

static int
u3g_detach(device_t dev)
{
    struct u3g_softc *sc = (struct u3g_softc *)device_get_softc(dev);
    uint8_t subunit;

    DPRINTF("sc=%p\n", sc);

    /* NOTE: It is not dangerous to detach more ports than attached! */
    ucom_detach(&sc->sc_super_ucom, sc->sc_ucom);

    for (subunit = 0; subunit != U3G_MAXPORTS; subunit++)
        usbd_transfer_unsetup(sc->sc_xfer[subunit], U3G_N_TRANSFER);

    if (ucom_unref(&sc->sc_super_ucom)) {
        mtx_destroy(&sc->sc_mtx);
    }

    return (0);
}

static void
u3g_free_softc(struct u3g_softc *sc)
{
    if (ucom_unref(&sc->sc_super_ucom)) {
        mtx_destroy(&sc->sc_mtx);
        free(sc);
    }
}

static void
u3g_free(struct ucom_softc *ucom)
{
    u3g_free_softc((struct u3g_softc *)ucom->sc_parent);
}

static void
u3g_start_read(struct ucom_softc *ucom)
{
    struct u3g_softc *sc = (struct u3g_softc *)ucom->sc_parent;

    /* start interrupt endpoint (if configured) */
    usbd_transfer_start(sc->sc_xfer[ucom->sc_subunit][U3G_INTR]);

    /* start read endpoint */
    usbd_transfer_start(sc->sc_xfer[ucom->sc_subunit][U3G_BULK_RD]);
}

static void
u3g_stop_read(struct ucom_softc *ucom)
{
    struct u3g_softc *sc = (struct u3g_softc *)ucom->sc_parent;

    /* stop interrupt endpoint (if configured) */
    usbd_transfer_stop(sc->sc_xfer[ucom->sc_subunit][U3G_INTR]);

    /* stop read endpoint */
    usbd_transfer_stop(sc->sc_xfer[ucom->sc_subunit][U3G_BULK_RD]);
}

static void
u3g_start_write(struct ucom_softc *ucom)
{
    struct u3g_softc *sc = (struct u3g_softc *)ucom->sc_parent;

    usbd_transfer_start(sc->sc_xfer[ucom->sc_subunit][U3G_BULK_WR]);
}

static void
u3g_stop_write(struct ucom_softc *ucom)
{
    struct u3g_softc *sc = (struct u3g_softc *)ucom->sc_parent;

    usbd_transfer_stop(sc->sc_xfer[ucom->sc_subunit][U3G_BULK_WR]);
}

static void
u3g_write_callback(struct usb_xfer *xfer, usb_error_t error)
{
    struct ucom_softc *ucom = (struct ucom_softc *)usbd_xfer_softc(xfer);
    struct usb_page_cache *pc = NULL;
    uint32_t actlen;
    uint32_t frame;

    UCOM_HANDLER_ITEM_S *p_u3g_write_handler = NULL;
    static uint32_t writelen;

    DPRINTFN(1, "\n");

    switch (USB_GET_STATE(xfer)) {
    case USB_ST_TRANSFERRED:

        p_u3g_write_handler = (UCOM_HANDLER_ITEM_S *)LOS_MemboxAlloc(m_aucTtyUsbHandlerPool);
        if (p_u3g_write_handler != NULL)
        {
            p_u3g_write_handler->length = writelen;
            if (LOS_QueueWrite(m_uwTtyUsbHandlerQueue, p_u3g_write_handler, sizeof(UINT32), LOS_NO_WAIT))
            {
                (VOID)LOS_MemboxFree(m_aucTtyUsbHandlerPool, p_u3g_write_handler);
            }
        }

    case USB_ST_SETUP: /*lint !e616*/
tr_setup:
        for (frame = 0; frame != U3G_TXFRAMES; frame++) {
            usbd_xfer_set_frame_offset(xfer, frame * U3G_TXSIZE, frame);

            pc = usbd_xfer_get_frame(xfer, frame);
            if (ucom_get_data(ucom, pc, 0, U3G_TXSIZE, &actlen) == 0)
                break;

            usbd_xfer_set_frame_len(xfer, frame, actlen);
            writelen = actlen;
        }
        if (frame != 0) {
            usbd_xfer_set_frames(xfer, frame);
            usbd_transfer_submit(xfer);
        }
        break;

    default:            /* Error */
        if (error != USB_ERR_CANCELLED) {
            /* do a builtin clear-stall */
            usbd_xfer_set_stall(xfer);
            goto tr_setup;
        }
        break;
    }
}

static void
u3g_read_callback(struct usb_xfer *xfer, usb_error_t error)
{
    struct ucom_softc *ucom = (struct ucom_softc *)usbd_xfer_softc(xfer);
    struct usb_page_cache *pc;
    int actlen;

    DPRINTFN(1, "\n");

    usbd_xfer_status(xfer, &actlen, (int *)NULL, (int *)NULL, (int *)NULL);

    switch (USB_GET_STATE(xfer)) {
    case USB_ST_TRANSFERRED:
        pc = usbd_xfer_get_frame(xfer, 0);
        ucom_put_data(ucom, pc, 0, actlen);

    case USB_ST_SETUP: /*lint !e616*/
tr_setup:
        usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
        usbd_transfer_submit(xfer);
        break;

    default:            /* Error */
        if (error != USB_ERR_CANCELLED) {
            /* do a builtin clear-stall */
            usbd_xfer_set_stall(xfer);
            goto tr_setup;
        }
        break;
    }
}

static void
u3g_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
{
    struct u3g_softc *sc = (struct u3g_softc *)ucom->sc_parent;

    *lsr = sc->sc_lsr[ucom->sc_subunit];
    *msr = sc->sc_msr[ucom->sc_subunit];
}

static void
u3g_cfg_set_line(struct ucom_softc *ucom)
{
    struct u3g_softc *sc = (struct u3g_softc *)ucom->sc_parent;
    struct usb_device_request req;

    req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
    USETW(req.wValue, sc->sc_line[ucom->sc_subunit]);
    req.wIndex[0] = sc->sc_iface[ucom->sc_subunit];
    req.wIndex[1] = 0;
    req.wLength[0] = 0;
    req.wLength[1] = 0;

    (void)ucom_cfg_do_request(sc->sc_udev, ucom,
        &req, NULL, 0, 1000);
}

static void
u3g_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
{
    struct u3g_softc *sc = (struct u3g_softc *)ucom->sc_parent;

    DPRINTF("onoff = %d\n", onoff);

    if (onoff)
        sc->sc_line[ucom->sc_subunit] |= UCDC_LINE_DTR;
    else
        sc->sc_line[ucom->sc_subunit] &= ~UCDC_LINE_DTR;

    u3g_cfg_set_line(ucom);
}

static void
u3g_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
{
    struct u3g_softc *sc = (struct u3g_softc *)ucom->sc_parent;

    DPRINTF("onoff = %d\n", onoff);

    if (onoff)
        sc->sc_line[ucom->sc_subunit] |= UCDC_LINE_RTS;
    else
        sc->sc_line[ucom->sc_subunit] &= ~UCDC_LINE_RTS;

    u3g_cfg_set_line(ucom);
}

static void
u3g_intr_callback(struct usb_xfer *xfer, usb_error_t error)
{
    struct ucom_softc *ucom = (struct ucom_softc *)usbd_xfer_softc(xfer);
    struct u3g_softc *sc = (struct u3g_softc *)ucom->sc_parent;
    struct usb_page_cache *pc;
    struct usb_cdc_notification pkt;
    int actlen;
    uint16_t wLen;
    uint8_t mstatus;
    int i;

    DPRINTFN(1, "\n");

    usbd_xfer_status(xfer, &actlen, (int *)NULL, (int *)NULL, (int *)NULL);

    switch (USB_GET_STATE(xfer)) {
    case USB_ST_TRANSFERRED:
        if (actlen < 8) {    /* usb_cdc_notification with 2 data bytes */
            DPRINTF("message too short (expected 8, received %d)\n", actlen);
            goto tr_setup;
        }
        pc = usbd_xfer_get_frame(xfer, 0);
        usbd_copy_out(pc, 0, &pkt, actlen);

        PRINTK("read intr data start -->\n\n");
        for(i=0; i<actlen; i++){
            PRINTK("[0x%x]  ", *((char*)pc->buffer+i));
        }
        PRINTK("\nread intr data end -->\n\n");

        wLen = UGETW(pkt.wLength);
        if (wLen < 2) {
            DPRINTF("message too short (expected 2 data bytes, received %d)\n", wLen);
            goto tr_setup;
        }

        if (pkt.bmRequestType == UCDC_NOTIFICATION
            && pkt.bNotification == UCDC_N_SERIAL_STATE) {
            /*
                 * Set the serial state in ucom driver based on
                 * the bits from the notify message
                 */
            DPRINTF("notify bytes = 0x%02x, 0x%02x\n",
                pkt.data[0], pkt.data[1]);

            /* currently, lsr is always zero. */
            sc->sc_lsr[ucom->sc_subunit] = 0;
            sc->sc_msr[ucom->sc_subunit] = 0;

            mstatus = pkt.data[0];

            if (mstatus & UCDC_N_SERIAL_RI)
                sc->sc_msr[ucom->sc_subunit] |= SER_RI;
            if (mstatus & UCDC_N_SERIAL_DSR)
                sc->sc_msr[ucom->sc_subunit] |= SER_DSR;
            if (mstatus & UCDC_N_SERIAL_DCD)
                sc->sc_msr[ucom->sc_subunit] |= SER_DCD;
            ucom_status_change(ucom);
        }

    case USB_ST_SETUP: /*lint !e616*/
tr_setup:
        usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
        usbd_transfer_submit(xfer);
        return;

    default:            /* Error */
        if (error != USB_ERR_CANCELLED) {
            /* try to clear stall first */
            usbd_xfer_set_stall(xfer);
            goto tr_setup;
        }
        return;
    }
}

static void
u3g_poll(struct ucom_softc *ucom)
{
    struct u3g_softc *sc = (struct u3g_softc *)ucom->sc_parent;
    usbd_transfer_poll(sc->sc_xfer[ucom->sc_subunit], U3G_N_TRANSFER);
}

#undef USB_DEBUG_VAR
